CS 149: Introduction to Programming
James Madison University, Fall 2017 Semester

Lab15: Debugging and tracing code

Background

Have you ever heard that unforgettable children's song 100 bottles of pop on the wall? Your task is to write a program that will "sing" a few verses of this song. Here's an example recording on Youtube, but beware it may get stuck in your head the rest of the day!

Objectives

Part 1: Bottles of Pop

Download Bottles.java as a starting point for the lab. Write your name and today's date in the Javadoc comment. Here is what the output should look like when you finish the lab:

100 bottles of pop on the wall
100 bottles of pop
If one of those bottles should happen to fall
99 bottles of pop on the wall

99 bottles of pop on the wall
99 bottles of pop
If one of those bottles should happen to fall
98 bottles of pop on the wall

(repeat until 1)
  1. Most of the algorithm has already been implemented in Bottles.java. Figure out how to complete the sing method.

  2. Test your solution on a small number of verses. You may want to run this lab from the command line (i.e., java Bottles 5) for convenience.

    • Is it working right? If not, go back and fix it!
    • Do you have a blank line after each verse?
  3. Modify your code to print "1 bottle" instead of "1 bottles" when applicable.

    • Don't forget the ending of the "2 bottles of pop" verse.
  4. Modify your code to print "No more bottles" instead of "0 bottles" when applicable.

  5. Make sure your solution works in the following cases. Test each one via the command line.

    • java Bottles 0   nothing should print
    • java Bottles 1   the last verse prints
    • java Bottles 5   only five verses print

Part 2: Debugging with jGRASP

Most integrated development environments (like DrJava, Eclipse, and NetBeans) come with a debugger that allows you to "step through" your code while it is running.

  1. Click the Build menu and make sure the box is checked for Debug Mode. It is the default so it should be, but if not click the box. Compile your program.

  2. Move your cursor to the following line of code in jGRASP:

    if (args.length > 0) {
  3. Use your mouse to hover over the gray line to the left of the line numbers of the statements until a red dot appears. When the red dot appears click to set a Breakpoint on that line. This will make the program stop when it comes to that line when run in Debug Mode.

  4. Now click the "Lady Bug" button on the toolbar. Your program should stop on the if statement.

  5. At this point, you can take any of the following steps from the menu of blue arrows on the left above threads:

    • Step Over (leftmost straight down blue arrow)-- run the current line of code, but don't step into other methods
    • Step In (middle blue arrow that points right) -- run the current line, stepping into any method calls
    • Step Out (right blue arrow that points left) -- run all code until it returns from the current method
    • Resume (blue > button to the right) -- run all code until it reaches another breakpoint (if any)
  6. Practice stepping through your code one line at a time. Each time the program ends, you will need to restart the debugger.

  7. jGRASP has an "Auto Step" feature that animates the entire execution of a program. Try setting it by clicking on the double down arrow. Note the slider bar that appears where you can set the time between steps. Now rerun your program with the initial breakpoint. You should be able to click the step button and watch your program run automatically, stepping through the program.

  8. Submit your completed Bottles.java file via Canvas.

  9. Now go to CodingBat and complete at a minimum three Recursion-1 problems, one of which should be sumDigits. Note two problems have solutions.

  10. While this is not due until Sunday, you should try to finish the whole thing today and ideally start Lab16.